package com.example.himalayaproject.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.himalayaproject.R;
import com.example.himalayaproject.play.noncontent.adapter.FragmentPlayAdapter;
import com.example.himalayaproject.play.noncontent.bean.PlayEntity;
import com.example.himalayaproject.play.noncontent.callback.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class PlayFragment extends Fragment implements Callback<PlayEntity>,FragmentPlayAdapter.onNextPageListener {
    private RecyclerView recyclerView;
    private FragmentPlayAdapter adapter;
    private  HttpService service;


    public PlayFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_play, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aboutRecyclerView(view);
    }

    private void aboutRecyclerView(View view) {
           service = new Retrofit.Builder()
                  .baseUrl(" http://mobile.ximalaya.com/")
                  .addConverterFactory(GsonConverterFactory.create())
                  .build()
                  .create(HttpService.class);
        service.getPlay(1,20,null).enqueue(this);
        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_play_recycler);
        adapter = new FragmentPlayAdapter(getContext());
        recyclerView.setAdapter(adapter);
        adapter.setListener(this);
    }

    @Override
    public void onResponse(Call<PlayEntity> call, Response<PlayEntity> response) {
         adapter.addAll(response.body().getList());
    }

    @Override
    public void onFailure(Call<PlayEntity> call, Throwable t) {

    }

    @Override
    public void onNextPage(int page) {
        service.getPlay(page,20,null).enqueue(this);
    }
}
